package com.next.aap.server.persistance.helper;

import java.util.List;

import org.springframework.stereotype.Component;

import com.next.aap.server.persistance.State;
import com.next.core.db.page.PageInfo;
import com.next.core.db.page.PageResult;
import com.next.core.db.page.QueryParam;
import com.next.core.db.validator.ObjectValidator;
import com.next.core.exception.AppException;
import com.next.ext.core.db.page.HibernateQueryParamPageInfo;
import com.next.ext.core.db.page.HibernateQueryParamPageInfo.ORDER;
import com.next.ext.core.persistance.dao.BasePersistanceDao;

@Component
public class StateHelper extends BasePersistanceDao<State> implements ObjectValidator<State> {

	/**
	 * Creates/updates a state in Database
	 * 
	 * @param state
	 * @return saved state
	 * @throws AppException
	 */
	public State saveState(State state) throws AppException
	{
		checkIfStateExistsWithSameName(state);
		state.setNameUp(state.getName().toUpperCase());
		state = super.saveObject(state);
		return state;
	}

	/**
	 * deletes a state in Database
	 * 
	 * @param state
	 * @return updated state
	 * @throws AppException
	 */
	public void deleteState(State state) throws AppException {
		super.deleteObject(state);
	}

	/**
	 * return a State with given primary key/id
	 * 
	 * @param id
	 * @return State with PK as id(parameter)
	 * @throws AppException
	 */
	public State getStateById(Long id) throws AppException
	{
		State state = super.getObjectByPK(State.class, id);
		return state;
	}

	/**
	 * @param pageInfo
	 * @return search result
	 * @throws AppException
	 */
	public PageResult<State> searchStates(PageInfo pageInfo) throws AppException
	{
		return super.findObject(State.class, pageInfo);
	}

	public List<State> getAllStates() throws AppException
	{
		HibernateQueryParamPageInfo pageInfo = new HibernateQueryParamPageInfo();
		pageInfo.addOrderBy("name", ORDER.ASC);
		return super.findObject(State.class, pageInfo).getResultList();
	}
	public void validateObjectForCreate(State state) throws AppException {
		checkIfStringMissing("Name", state.getName());
	}

	public void validateObjectForUpdate(State state) throws AppException {
		checkIfStringMissing("Name", state.getName());
	}
	private void checkIfStateExistsWithSameName(State state) throws AppException{
		HibernateQueryParamPageInfo pageInfo = new HibernateQueryParamPageInfo();
		QueryParam emailQueryParam = new QueryParam();
		emailQueryParam.setCaseSenstive(false);
		emailQueryParam.setField("nameUp");
		emailQueryParam.setFieldType(QueryParam.FIELD_TYPE_STRING);
		emailQueryParam.setOperator(QueryParam.OPERATOR_EQUAL);
		emailQueryParam.setValue(state.getName().toUpperCase());
		pageInfo.addCriteria(emailQueryParam);
		
		if(state.getId() != null && state.getId() > 0){
			QueryParam stateIdQueryParam = new QueryParam();
			stateIdQueryParam.setCaseSenstive(false);
			stateIdQueryParam.setField("id");
			stateIdQueryParam.setFieldType(QueryParam.FIELD_TYPE_NUMBER);
			stateIdQueryParam.setOperator(QueryParam.OPERATOR_NOT_EQUAL);
			stateIdQueryParam.setValue(state.getId());
			pageInfo.addCriteria(stateIdQueryParam);
		}
		
		pageInfo.addOrderBy("name", ORDER.ASC);
		
		PageResult<State> pr = this.searchStates(pageInfo);
		if(pr.getResultList().size() > 0){
			throw new AppException("State already exists with name = "+state.getName());
		}
	}
}